package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.OnelevelMenu;
import com.example.buildtable.service.OnelevelMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "一级菜单")
@RequestMapping(value = "/onelevelMenu/")
public class OnelevelMenuController {
    @Autowired
    private OnelevelMenuService onelevelMenuService;
    
    @Operation(summary = "查询所有一级菜单数据")
    @GetMapping(value = "/getAllOnelevelMenu")
    public R getAllOnelevelMenu() {
        List<OnelevelMenu> onelevelMenuList = onelevelMenuService.list();
        return R.ok("查询所有一级菜单数据成功").data("onelevelMenuList", onelevelMenuList);
    }
    
    @Operation(summary = "增加一级菜单")
    @PostMapping(value = "/addOnelevelMenu")
    public R addOnelevelMenu(OnelevelMenu onelevelMenu) {
        boolean flag = onelevelMenuService.save(onelevelMenu);
        if (flag) {
            return R.ok("增加一级菜单成功");
        } else {
            return R.ok("增加一级菜单失败");
        }
    }

    @Operation(summary = "删除一级菜单")
    @GetMapping(value = "/deleteOnelevelMenu/{onelevelMenuId}")
    public R deleteOnelevelMenu(@PathVariable("onelevelMenuId") Integer onelevelMenuId) {
        boolean flag = onelevelMenuService.removeById(onelevelMenuId);
        if (flag) {
            return R.ok("删除一级菜单成功");
        } else {
            return R.ok("删除一级菜单失败");
        }
    }

    @Operation(summary = "按照一级菜单查看")
    @GetMapping(value = "/getOnelevelMenuById/{onelevelMenuId}")
    public R getOnelevelMenuById(@PathVariable("onelevelMenuId") Integer onelevelMenuId) {
        OnelevelMenu onelevelMenu = onelevelMenuService.getById(onelevelMenuId);
        if (onelevelMenu != null) {
            return R.ok("按照一级菜单查看成功").data("onelevelMenu", onelevelMenu);
        } else {
            return R.error("按照一级菜单查看失败");
        }
    }

    @Operation(summary = "修改一级菜单")
    @PostMapping(value = "/updateOnelevelMenu")
    public R updateOnelevelMenu(OnelevelMenu onelevelMenu) {
        boolean flag = onelevelMenuService.updateById(onelevelMenu);
        if (flag) {
            return R.ok("修改一级菜单成功");
        } else {
            return R.ok("修改一级菜单失败");
        }
    }
}